package org.kzwl.service.web.impl;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.entity.web.CuData;
import org.kzwl.entity.web.CuVisit;
import org.kzwl.mapper.web.CuVisitMapper;
import org.kzwl.service.web.CuVisitService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;

/**
 *
 */
@Service
public class CuVisitServiceImpl extends ServiceImpl<CuVisitMapper, CuVisit>
        implements CuVisitService {

    @Override
    public CuVisit getCuVisit() {
        CuVisit cuVisit = getOne(new LambdaQueryWrapper<CuVisit>().eq(CuVisit::getCuVisitId, 1));
        if (null == cuVisit) {
            cuVisit = new CuVisit().setCuVisitId(1);
            save(cuVisit);
            cuVisit = getOne(new LambdaQueryWrapper<CuVisit>().eq(CuVisit::getCuVisitId, 1));
        }
        return cuVisit;
    }

    @Override
    public void addCount(CuVisit cuVisit) {
        CuVisit visit = getCuVisit();
        Field[] fields = ReflectUtil.getFields(cuVisit.getClass());
        for (Field field : fields) {
            if (null != ReflectUtil.getFieldValue(cuVisit, field)) {
                Integer value = (Integer) ReflectUtil.getFieldValue(visit,field.getName());
                ReflectUtil.setFieldValue(visit, field, value + 1);
                this.updateById(visit);
            }
        }
    }
}




